package com.example.smartcity.Controller;

import com.alibaba.fastjson.JSONObject;
import com.example.smartcity.Service.NewsCommentService;
import com.example.smartcity.Service.NewsService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
public class NewsController {
    @Autowired
    @Qualifier(value = "NewsService")
    private NewsService newsService;
    @Autowired
    @Qualifier(value = "NewsCommentService")
    private NewsCommentService newsCommentService;

    @RequestMapping(value = "/news/research",method = RequestMethod.GET)
    public String findNews(@RequestParam("keyword")String searchword){
        return newsService.findNewsByTitleOrContent(searchword);
    }
    @RequestMapping(value = "/news/findnewsbycategory",method = RequestMethod.GET)
    public String findNewsByCategory(@RequestParam("category")String category){
        return newsService.findNewsByCategory(category);
    }
    @RequestMapping(value = "/news/addViews")
    public void updateViews(@RequestBody int id){
        newsService.UpdateViewsNumber(id);
    }
    @RequestMapping(value = "/news/subscribe")
    public void updateLikes(@RequestBody int id){
        newsService.UpdateLikesNumber(id);
    }
    @RequestMapping(value ="/news/getnews")
    public String getNews(HttpServletResponse response){return newsService.findAll();}
    @RequestMapping(value = "/iscollect",method = RequestMethod.POST)
    public String isCollect(@Param("user_id")String user_id,@Param("title")String title){
        JSONObject map = new JSONObject();
        map.put("code" ,200);
        map.put("msg","succeess");
        int iscollect = newsService.iscollect(Integer.valueOf(user_id),title);
        map.put("iscollect",iscollect);
        System.out.println(map.toJSONString());
        return map.toJSONString();
    }
    @RequestMapping(value = "/addcollect",method = RequestMethod.POST)
    public String addcollect(@Param("user_id")String user_id,@Param("title")String title){
        JSONObject map = new JSONObject();


        System.out.println(user_id+"+"+title);
        if (Integer.valueOf(user_id)==1){
            map.put("code" ,"500");
            map.put("msg","failed");
            return map.toJSONString();
        }
        newsService.addcollect(Integer.valueOf(user_id),title);
        map.put("code" ,"200");
        map.put("msg","succeess");
        System.out.println(map.toJSONString());
        return map.toJSONString();
    }
    @RequestMapping(value = "/deletecollect")
    public String deletcollect(@Param("user_id")String user_id,@Param("title")String title){
        JSONObject map = new JSONObject();
        map.put("code" ,"200");
        map.put("msg","succeess");
        newsService.cancelcollect(Integer.valueOf(user_id),title);
        System.out.println(map.toJSONString());
        return map.toJSONString();
    }
    @RequestMapping(value = "/getcollect",method = RequestMethod.GET)
    public String getcollect(@Param("user_id")int user_id){
        return newsService.getCollection(user_id);
    }
    @RequestMapping(value = "/press/addview")
    public void addview(@Param("textname")String title){
        int id = newsCommentService.findNewsIdByTitle(title);
        System.out.println(id);
        newsService.addViewNumber(id);
    }






}
